import { defineConfig } from "vite";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
// 配置Element Plus 按需导入
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
// 把你需要的组件的 css 或者 sass 文件自动引入进来
import ElementPlus from "unplugin-element-plus/vite";
import vue from "@vitejs/plugin-vue";
import autoprefixer from "autoprefixer";
import path from "path";

// https://vitejs.dev/config/
export default defineConfig({
  build: {
    outDir: "dist",
    assetsDir: "assets",
    chunkSizeWarningLimit: 1500,
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.includes("node_modules")) {
            return id
              .toString()
              .split("node_modules/")[1]
              .split("/")[0]
              .toString();
          }
        },
      },
    },
  },
  plugins: [
    vue(),
    AutoImport({
      imports: ["vue", "vue-router"],
      resolvers: [ElementPlusResolver()],
      dts: "src/types/auto-import.d.ts",
    }),
    Components({
      resolvers: [ElementPlusResolver()],
      dts: "src/types/components.d.ts",
    }),
    ElementPlus({}),
  ],
  server: {
    host: "127.0.0.1",
    port: 9527,
    cors: true,
    proxy: {
      "/api": {
        target: "http://localhost:8000/",
        changeOrigin: true,
      },
    },
  },
  resolve: {
    alias: {
      // 设置'@' 指向 'src 目录(
      "@": path.join(__dirname, "./src"),
    },
  },
  css: {
    postcss: {
      plugins: [autoprefixer()],
    },
  },
});
